﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.MSTestV2.CLIAutomation;

namespace MSTest.VstestConsoleWrapper.IntegrationTests;

[TestClass]
public class TimeoutTests : CLITestBase
{
    private const string TestAssetName = "TimeoutTestProject";

    [TestMethod]
    [DataRow("net462")]
    [DataRow("net8.0")]
    public void ValidateTimeoutTests(string targetFramework)
    {
        InvokeVsTestForExecution([TestAssetName], testCaseFilter: "TimeoutTest|RegularTest", targetFramework: targetFramework);

        ValidateFailedTestsCount(targetFramework == "net462" ? 5 : 4);

        var failedTests = new List<string>
        {
            "TimeoutTestProject.TimeoutTestClass.TimeoutTest_WhenUserCancelsTestContextToken_AbortTest",
            "TimeoutTestProject.TimeoutTestClass.RegularTest_WhenUserCancelsTestContextToken_TestContinues",
            "TimeoutTestProject.TimeoutTestClass.TimeoutTest_WhenTimeoutReached_CancelsTestContextToken",
            "TimeoutTestProject.TimeoutTestClass.TimeoutTest_WhenTimeoutReached_ForcesTestAbort",
        };

        if (targetFramework == "net462")
        {
            failedTests.Add("TimeoutTestProject.TimeoutTestClass.TimeoutTest_WhenUserCallsThreadAbort_AbortTest");
        }

        ValidateFailedTestsContain(false, [.. failedTests]);

        // We should find the <TargetFramework>/TimeoutTestOutput.txt file, as it's our way to validate
        // that when the timeout expires it cancels the test context token.
        string timeoutFile = Path.Combine(
            GetAssetFullPath(TestAssetName, targetFramework: targetFramework),
            "..",
            "TimeoutTestOutput.txt");
        Assert.IsTrue(File.Exists(timeoutFile), $"Timeout output file not found: {timeoutFile}");
    }
}
